﻿using SHKF.B2C.Mannage.AdminUI.Model;
using SHKF.B2C.Mannage.AdminUI.Network;
using SHKF.B2C.YouZanYun.Model.Inter.Image;
using SHKF.B2C.YouZanYun.Model.Inter.MaterialCategory;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WQDSkin.Forms.Controls;

namespace SHKF.B2C.Mannage.AdminUI.View.Yzy.Import
{
    /// <summary>
    /// frmSearchImage.xaml 的交互逻辑
    /// </summary>
    public partial class frmSearchImage : Window
    {
        private ImportImageViewModel data;
        private HttpClientYouZanYun httpYzy;
        public int SelectCount = 3;

        public Dictionary<long, List<ImageItem>> tempImageList = new Dictionary<long, List<ImageItem>>();

        /// <summary>
        /// 选择的图片集合
        /// </summary>
        public List<ImageItem> SelectedImages = new List<ImageItem>();

        public string authority_id = "";

        public frmSearchImage()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.httpYzy = GL.CreateYouZanYun();
            this.httpYzy.authority_id = this.authority_id;
 
            this.data = new ImportImageViewModel();
            this.DataContext = this.data;

            this.LoadImageCategory();
        }


        private void btnOK_Click(object sender, RoutedEventArgs e)
        { 
            if (this.dgvImage.SelectedItems.Count >= SelectCount)
            {
                this.SelectedImages.Clear();
                foreach (ImageItem imageItem in this.dgvImage.SelectedItems)
                {
                    this.SelectedImages.Add(imageItem);
                }

                this.DialogResult = true;
                this.Close();
            }
            else
            {
                MessageBox.Show($"最少选择{SelectCount}张默认图片");
            }

        }

        private void tvCategory_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var data = this.tvCategory.SelectData;
            if (data != null && data is MaterialCategoryData)
            {
                MaterialCategoryData data1 = (MaterialCategoryData)data;

                //加载图片
                long category_id = data1.category_id;
                this.dgvImage.ItemsSource = this.tempImageList[category_id];
            }
        }

      
        //加载第一页
        private async void LoadPage1Image(long category_id)
        {
            //加载图片
            ImageResult result = await this.httpYzy.GetMaterialImageList(category_id);
            if (result.Items != null)
            {
                int total_count = result.paginator.total_count;
                int page_size = result.paginator.page_size;

                int page_count = (int)Math.Ceiling((double)total_count / (double)page_size);
                this.data.ImageCurrentValue += result.Items.Count;

                if (this.tempImageList.ContainsKey(category_id))
                {
                    List<ImageItem> list = this.tempImageList[category_id];
                    list.AddRange(result.Items.ToArray());
                }
                else
                {
                    this.tempImageList.Add(category_id, result.Items);
                }

                if (page_count > 1)
                {
                    LoadPageAllImage(page_count, category_id);
                }
            }
        }

        private async void LoadPageAllImage(int PageCount, long category_id)
        {
            for (int i = 2; i <= PageCount; i++)
            {
                //加载图片
                ImageResult result = await this.httpYzy.GetMaterialImageList(category_id, i);
                if (result.Items != null)
                {
                    if (this.tempImageList.ContainsKey(category_id))
                    {
                        List<ImageItem> list = this.tempImageList[category_id];
                        list.AddRange(result.Items.ToArray());
                    }

                    this.data.ImageCurrentValue += result.Items.Count;
                }
            }
        }
         
        private async void LoadImageCategory()
        {
            this.tvCategory.Items.Clear();

            int count = 0; //总图片数

            //未分组
            MaterialCategoryResult result = await this.httpYzy.GetMaterialCategory(2);
            if (result.data != null && result.data.Count > 0)
            {
                WQDTreeViewItem item2 = new WQDTreeViewItem();
                foreach (MaterialCategoryData data in result.data)
                {
                    item2.DataContext = data;
                    item2.Header = result.data[0].category_name + $"({data.count})";
                    tvCategory.Items.Add(item2);
                    this.data.MaterialCategoryList.Add(data);

                    count += data.count;
                }
            }

            //自定义
            result = await this.httpYzy.GetMaterialCategory(1);
            if (result.data != null && result.data.Count > 0)
            {
                WQDTreeViewItem item1 = new WQDTreeViewItem();
                item1.Header = "自定义分组";
                tvCategory.Items.Add(item1);

                foreach (MaterialCategoryData data in result.data)
                {
                    WQDTreeViewItem item3 = new WQDTreeViewItem();
                    item3.DataContext = data;
                    item3.Header = data.category_name + $"({data.count})";
                    item1.Items.Add(item3);

                    count += data.count;
                    this.data.MaterialCategoryList.Add(data);
                }

                item1.IsExpanded = true;
            }

            this.data.ImageMaximum = count;
            this.data.CategoryCurrentValue = 0;
            this.data.CategoryMaximum = this.data.MaterialCategoryList.Count;

            //加载图片

            await Task.Factory.StartNew(() => {

                foreach (MaterialCategoryData categoryData in this.data.MaterialCategoryList)
                {
                    Thread.Sleep(200);
                    LoadPage1Image(categoryData.category_id);
                    this.data.CategoryCurrentValue++;
                }

            });

        }

        private void dgvImage_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }
    }
}
